package com.tanhua.server.controller;

import com.tanhua.common.vo.ErrorResult;
import com.tanhua.server.service.QXMomentsService;
import com.tanhua.server.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("comments")
public class CommentController {

    @Autowired
    private QXMomentsService QXMomentsService;

    /**
     * 评论列表
     *
     * @param publishId
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping
    public ResponseEntity<PageResult> commentList(@RequestParam("movementId") String publishId,
                                                  @RequestParam(value = "page", defaultValue = "1") Integer page,
                                                  @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        try {
            PageResult pageResult = QXMomentsService.findCommentList(publishId, page, pageSize);
            if (null != pageResult) {
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 发表评论
     *
     * @param
     * @return
     */
    @PostMapping
    public ResponseEntity<Object> sendComment(@RequestBody Map<String, String> param) {
        ErrorResult errorResult = null;
        try {
            String publishId = param.get("movementId");//动态ID
            String content = param.get("comment");//评论内容
            Boolean result = QXMomentsService.sendComment(publishId, content);
            if (result) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
           e.printStackTrace();
        }
        errorResult = ErrorResult.builder().errCode("000005").errMessage("账号封禁中,无法对动态操作!").build();
        //如果返回值为空,则说明验证失败,返回状态码500并将对应错误信息传递给用户
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }

    /**
     * 点赞评论
     *
     * @param commentId 评论的ID
     * @return
     */
    @GetMapping("{id}/like")
    public ResponseEntity<Object> likeComment(@PathVariable("id") String commentId) {
        try {
            Long like = QXMomentsService.likeComment(commentId);
            if (null != like) {
                return ResponseEntity.ok(like);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        ErrorResult errorResult = ErrorResult.builder().errCode("000005").errMessage("账号封禁中,无法对动态操作!").build();
        //如果返回值为空,则说明验证失败,返回状态码500并将对应错误信息传递给用户
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }

    @GetMapping("{id}/dislike")
    public ResponseEntity<Object> disLikeComment(@PathVariable("id") String commentId) {
        try {
            Long disLike = QXMomentsService.disLikeComment(commentId);
            if (null != disLike) {
                return ResponseEntity.ok(disLike);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        ErrorResult errorResult = ErrorResult.builder().errCode("000005").errMessage("账号封禁中,无法对动态操作!").build();
        //如果返回值为空,则说明验证失败,返回状态码500并将对应错误信息传递给用户
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }
}
